package jee.boot.module.bpm.service;

import jee.boot.common.basic.CrudService;
import jee.boot.common.basic.ServiceException;
import jee.boot.common.utils.JsonMapper;
import jee.boot.common.utils.StringUtils;
import jee.boot.entity.auth.Account;
import jee.boot.entity.auth.Resource;
import jee.boot.entity.system.Filter;
import jee.boot.module.bpm.dao.FormDefinitionDao;
import jee.boot.entity.bpm.BusTableRel;
import jee.boot.entity.bpm.BusinessObject;
import jee.boot.entity.bpm.BusinessTable;
import jee.boot.entity.bpm.FormDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 表单定义
 */
@Service
public class FormDefinitionService extends CrudService<FormDefinitionDao, FormDefinition> {
    @Autowired
    private BusinessObjectService businessObjectService;
    @Override
    public FormDefinition get(FormDefinition entity) {
        FormDefinition formDefinition = super.get(entity);
        loadCompleteData(formDefinition);
        return formDefinition;
    }

    @Override
    public void save(FormDefinition entity) {
        uniquenessCheck(entity,"name");
        super.save(entity);
    }

    @Override
    public void update(FormDefinition entity) {
        uniquenessCheck(entity,"name");
        super.update(entity);
    }
    /**
     * 根据名称查询表单设计的完整信息,包含数据对象,数据关联关系以及所有的数据实体结构
     * @param definition
     * @return
     */
    public FormDefinition getCompleteDataByName(FormDefinition definition) {
        definition.addFilter(new Filter("name","=",definition.getName()));
        List<FormDefinition> list = dao.findList(definition);
        if(list!=null && !list.isEmpty()){
            FormDefinition formDefinition = list.get(0);
            loadCompleteData(formDefinition);
            return formDefinition;
        }
        return null;
    }

    /**
     * 加载完整信息
     * @param definition
     */
    public void loadCompleteData(FormDefinition definition){
        // 这个已经通过join的方式加载完成了,就不需要在重新查询了
        BusinessObject businessObject = definition.getBusinessObject();
        businessObjectService.loadCompleteData(businessObject);

    }


    /**
     * 按名称查询表单定义
     * @param entity
     * @return
     */
    public FormDefinition getByName(FormDefinition entity) {
        FormDefinition query=new FormDefinition();
        query.addFilter(new Filter("name", "=", entity.getName()));
        List<FormDefinition> list = dao.findList(query);
        if(list!=null && !list.isEmpty()){
            return list.get(0);
        }
        throw new ServiceException();
    }

    public void publish(FormDefinition entity) {
        // 以前发布过就删除吧
        if(StringUtils.isNotEmpty(entity.getPublish())){
            List<String> idList = Arrays.asList(entity.getPublish().split(","));
            Resource delParam = new Resource();
            delParam.setIdList(idList);
            remoteEndpoint.executePost(projectConfig.getOpenOauthServer(), "/resource/batch/delete", delParam, null);
        }
        // 保存主菜单
        Resource mainMenuResource = remoteEndpoint.executePost(projectConfig.getOpenOauthServer(), "/resource/save", entity.getResource(), Resource.class);
        // 设置下面的按钮
        entity.getResourceButtonList().stream().forEach(item -> {
            item.setParent(mainMenuResource);
        });
        // 保存下按钮
        List<Resource> buttonResourceList = remoteEndpoint.executePost(projectConfig.getOpenOauthServer(), "/resource/batch/save", entity.getResourceButtonList(), Resource.class);
        // 保存下这些菜单的ID
        buttonResourceList.add(mainMenuResource);
        FormDefinition formDefinition = new FormDefinition();
        formDefinition.setId(entity.getId());
        formDefinition.setFormStatus(entity.getFormStatus());
        formDefinition.setPublish(StringUtils.join(buttonResourceList.stream().map(item -> item.getId()).collect(Collectors.toList()), ","));
        this.dao.updatePublish(formDefinition);
    }

}
